// utils/response.js
class ResponseUtil {
  /**
   * 成功响应
   * @param {*} data 数据
   * @param {string} message 消息
   * @returns {object}
   */
  static success(data = null, message = 'success') {
    return {
      code: 0,
      message,
      data,
      success: true,
      timestamp: Date.now(),
    }
  }

  /**
   * 分页成功响应
   * @param {Array} list 数据列表
   * @param {number} total 总条数
   * @param {number} current 当前页
   * @param {number} pageSize 每页大小
   * @param {string} message 消息
   * @returns {object}
   */
  static pagination(list, total, current = 1, pageSize = 10, message = 'success') {
    return {
      code: 0,
      message,
      data: list,
      total: Number.parseInt(total),
      current: Number.parseInt(current),
      pageSize: Number.parseInt(pageSize),
      totalPages: Math.ceil(total / pageSize),
      hasMore: current * pageSize < total,
      success: true,
      timestamp: Date.now(),
    }
  }

  /**
   * 错误响应
   * @param {string} message 错误消息
   * @param {number} code 错误码
   * @param {*} errors 错误详情
   * @returns {object}
   */
  static error(message = '服务器内部错误', code = 500, errors = null) {
    const response = {
      code,
      message,
      success: false,
      timestamp: Date.now(),
    }

    if (errors) {
      response.errors = errors
    }

    return response
  }

  /**
   * 自定义业务响应
   * @param {number} code 业务码
   * @param {string} message 消息
   * @param {*} data 数据
   * @returns {object}
   */
  static custom(code, message, data = null) {
    return {
      code,
      message,
      data,
      success: code === 0,
      timestamp: Date.now(),
    }
  }
}

module.exports = ResponseUtil
